import express from "express";
import http from "http";
import { Server } from "socket.io";
import { ServerType } from "./type";

class App {
  private app = express();
  private server = http.createServer(this.app);
  private port: number;
  private io: ServerType = new Server(this.server, {
    cors: { origin: "http://localhost:5173", credentials: true },
  });

  constructor(port: number) {
    this.port = port;
  }
  public start() {
    this.server.listen(this.port, () => {
      console.log(`Server listening on ${this.port}`);
    });

    this.io.on("connection", (socket) => {
      console.log(`${socket.id} join session`);
      socket.on("disconnect", () => {
        console.log(`${socket.id} leave session`);
      });
      socket.on("chat_msg", (msg) => {
        console.log(`message: ${msg}`);
        this.io.emit("chat_msg", msg);
      });
    });
  }
}

new App(3000).start();
